package com.wjf.accesscontrol.util.handler

import android.os.Handler
import android.os.Looper
import android.os.Message
import com.wjf.accesscontrol.ui.main.MainFragment
import com.wjf.accesscontrol.util.sdkUtils.TelpoUtils

const val WHAT_CODE_0 = 0
const val WHAT_CODE_1 = 1
const val TIME_1500 = 1500L
const val TIME_15000 = 15000L
class HandlerUtil private constructor(): Handler(Looper.getMainLooper()) {

    companion object{
        val instance: HandlerUtil by lazy(LazyThreadSafetyMode.NONE) { HandlerUtil() }
    }

    override fun handleMessage(msg: Message) {

        when (msg.what) {

            WHAT_CODE_0 ->{
                MainFragment.handlerCallback?.handlerCallback()
            }

            WHAT_CODE_1 ->{
                TelpoUtils.setRelayPowerOff()
            }
        }
    }

    /**
     * 开启
     */
    fun startHandler(){
        val obtain = Message.obtain()
        obtain.what = WHAT_CODE_0
        instance.removeMessages(WHAT_CODE_0)
        instance.sendMessageDelayed(obtain, TIME_1500)
    }

    fun doorLock(){
        val obtain = Message.obtain()
        obtain.what = WHAT_CODE_1
        instance.removeMessages(WHAT_CODE_1)
        instance.sendMessageDelayed(obtain, TIME_15000)
    }

    /**
     * 移除指定消息
     */
    fun removeMsg(){
        instance.removeMessages(WHAT_CODE_0)
    }

    /**
     * 移除所有消息
     */
    fun clearMsg(){
        instance.removeCallbacksAndMessages(null)
    }
}